package fr.labri.buganalyzer.writers;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.service.ServiceRegistry;

import fr.labri.buganalyzer.extractors.AbstractExtractor;
import fr.labri.buganalyzer.model.Developer;
import fr.labri.buganalyzer.model.Issue;

public abstract class AbstractAdapter {
	
	protected SessionFactory sessionFactory;
	protected ServiceRegistry serviceRegistry;
	protected Session session;
	protected String dbFile;
	Set<Developer> knownDevelopers;
	List<Issue> issues ;
	
	protected AbstractAdapter(String dbFile){
		this.dbFile = dbFile;
		knownDevelopers = new HashSet<Developer>();
		issues = new ArrayList<Issue>();
	}
	
	protected abstract SessionFactory configureSessionFactory();
	
	protected abstract void initSession();
	
	protected abstract void save(AbstractExtractor e);
	
	public void commit() {
		this.session.getTransaction().commit();
	}
	
	protected boolean isSaved(Developer dev){
		boolean result = false;
		for( Developer d : knownDevelopers )
			if(d.getAlias() != null && d.getAlias().equals(dev.getAlias())) 
				result= true;
		return result;
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public ServiceRegistry getServiceRegistry() {
		return serviceRegistry;
	}

	public void setServiceRegistry(ServiceRegistry serviceRegistry) {
		this.serviceRegistry = serviceRegistry;
	}

	public Session getSession() {
		return session;
	}

	public void setSession(Session session) {
		this.session = session;
	}

	public String getDbFile() {
		return dbFile;
	}

	public void setDbFile(String dbFile) {
		this.dbFile = dbFile;
	}

	public Set<Developer> getKnownDevelopers() {
		return knownDevelopers;
	}

	public void setKnownDevelopers(Set<Developer> knownDevelopers) {
		this.knownDevelopers = knownDevelopers;
	}

	public List<Issue> getIssues() {
		return issues;
	}

	public void setIssues(List<Issue> issues) {
		this.issues = issues;
	}

	public void loadIssues() {
		// TODO Auto-generated method stub
		
	}
}
